package com.crm.controller;

import com.crm.aonnation.Log;
import com.crm.dto.CourseDto;
import com.crm.pojo.Course;
import com.crm.result.PageResult;
import com.crm.result.ResponseResult;
import com.crm.result.Result;
import com.crm.service.CourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;

@Slf4j
@RequestMapping("/clues/course")
@RestController
@Api(tags = "课程管理相关接口")
public class CourseController {
    @Autowired
    private CourseService courseService;

    @ApiOperation("分页查询")
    @GetMapping("/list")
    public Result getCourse(
            String name, String code, String subject, BigDecimal price, String applicablePerson,
            @RequestParam(value = "params[beginCreateTime]", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate beginCreateTime,
            @RequestParam(value = "params[endCreateTime]", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endCreateTime
    ) {
        CourseDto dto = new CourseDto(name, code, subject, price, applicablePerson, null, beginCreateTime, endCreateTime);
        PageResult pageResult = courseService.getCourse(dto);
        return Result.commonSuccess(pageResult);
    }

    @ApiOperation("添加课程")
    @PostMapping
    @Log
    public ResponseResult addCourse(@RequestBody CourseDto dto) {
        log.info("添加课程参数");
        courseService.addCourse(dto);
        return ResponseResult.success();
    }


    @ApiOperation("删除课程")
    @DeleteMapping("/{id}")
    @Log
    public ResponseResult deleteCourse(@PathVariable Integer id) {
        log.info("根据id删除");
        courseService.deleteCourse(id);
        return ResponseResult.success();
    }

    @GetMapping("/listselect")

    public Result queryCourseList() {
        log.info("查询课程信息");
        List<Course> course = courseService.queryList();
        return Result.success(course);
    }

    @ApiOperation("查询回显")
    @GetMapping("/{id}")
    public Result queryById(@PathVariable Integer id) {
        log.info("根据id查询{}", id);
        Course result = courseService.queryById(id);
        return Result.success(result);
    }

    @ApiOperation("修改课程")
    @PutMapping
    @Log
    public ResponseResult updateCourse(@RequestBody Course course) {
        log.info("修改的参数{}", course);
        courseService.updateCourse(course);
        return ResponseResult.success();
    }


}
